﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Policy
{
  public abstract class TargetItemBaseReadWrite : XacmlElement
  {
    private TargetMatchCollectionReadWrite _match = new TargetMatchCollectionReadWrite();

    public virtual TargetMatchCollectionReadWrite Match
    {
      get
      {
        return this._match;
      }
      set
      {
        this._match = value;
      }
    }

    protected TargetItemBaseReadWrite(XacmlVersion schemaVersion)
      : base(XacmlSchema.Policy, schemaVersion)
    {
    }

    protected TargetItemBaseReadWrite(TargetMatchCollectionReadWrite match, XacmlVersion schemaVersion)
      : this(schemaVersion)
    {
      this._match = match;
    }

    protected TargetItemBaseReadWrite(XmlReader reader, string targetItemNodeName, string matchNodeName, XacmlVersion schemaVersion)
      : this(schemaVersion)
    {
      if (reader.LocalName == targetItemNodeName && this.ValidateSchema(reader, schemaVersion))
      {
        while (reader.Read())
        {
          if (reader.LocalName == matchNodeName && this.ValidateSchema(reader, schemaVersion))
            this._match.Add(this.CreateMatch(reader));
          if (reader.LocalName == targetItemNodeName && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }

    protected abstract TargetMatchBaseReadWrite CreateMatch(XmlReader reader);
  }
}
